<?php
/**
 * User: Do
 * Date: 7/4/14
 * Time: 11:33 AM
 */
class Model_admin extends Model
{
    function __construct()
    {
        parent::__construct();
    }

    //users
    public function getUsers(){

    }

    public function getUserByUsernamePassword($username, $password){
        try{
            $sth = $this->db->prepare("select * from users where username like :username and password like :password and level = 1");
            $sth->execute(array(
                ':username' => $username,
                ':password' => $password
            ));
            return $sth->fetchAll();
        }
        catch (Exception $e){
            throw new Exception("Saving error! Try again!", 201);
        }
    }

    //article

    //categories
    public function getCategories(){
        try{
            $sth = $this->db->prepare("select * from categories");
            $sth->execute();
            return $sth->fetchAll();
        }
        catch (Exception $e){
            throw new Exception("Saving error! Try again!", 201);
        }
    }

    public function getCategoryParent(){
        try{
            $sth = $this->db->prepare("select * from categories where category_parent = 0");
            $sth->execute();
            return $sth->fetchAll();
        }
        catch (Exception $e){
            throw new Exception("Saving error! Try again!", 201);
        }
    }

    public function getCategoryByParentId($parent){
        try{
            $sth = $this->db->prepare("select * from categories where category_parent=:category_parent");
            $sth->execute(array(
                ':category_parent' => $parent
            ));
            return $sth->fetchAll();
        }
        catch (Exception $e){
            throw new Exception("Saving error! Try again!", 201);
        }
    }

    public function getCategoryById(){

    }

    public function addCategory($category_title, $category_url, $category_parent, $category_position, $category_description){
        try{
            $sth = $this->db->prepare("insert into categories(`category_title`, `category_url`,`category_parent`, `category_position`, `category_description`)
            values( :category_title, :category_url, :category_parent, :category_position, :category_description)");
            $sth->execute(array(
                ':category_title' => $category_title,
                ':category_url' => $category_url,
                ':category_parent' => $category_parent,
                ':category_position' => $category_position,
                ':category_description' => $category_description
            ));
            return $this->db->lastInsertId();
        }
        catch (Exception $e){
            throw new Exception("Saving error! Try again!", 201);
        }
    }

    public function updateCategoryById(){

    }

    public function deleteCategoryById(){

    }

    //themes


    //Images
    public function getUploader(){
        try{
            $sth = $this->db->prepare("select * from uploader where utid = 0");
            $sth->execute();
            return $sth->fetchAll();
        }
        catch (Exception $e){
            throw new Exception("Saving error! Try again!", 201);
        }
    }

    public function getUploaderByFolder($utid){
        try{
            $sth = $this->db->prepare("select * from uploader where utid =:utid");
            $sth->execute(array(
                ':utid' => $utid
            ));
            return $sth->fetchAll();
        }
        catch (Exception $e){
            throw new Exception("Saving error! Try again!", 201);
        }
    }

    public function addUploader($utid, $upload_name, $upload_title, $upload_caption, $upload_alt, $upload_description, $upload_time){
        try{
            $sth = $this->db->prepare("insert into uploader(`utid`, `upload_name`,`upload_title`, `upload_caption`, `upload_alt`, `upload_description`, `upload_time`)
            values( :utid, :upload_name, :upload_title, :upload_caption, :upload_alt, :upload_description, :upload_time)");
            $sth->execute(array(
                ':utid' => $utid,
                ':upload_name' => $upload_name,
                ':upload_title' => $upload_title,
                ':upload_caption' => $upload_caption,
                ':upload_alt' => $upload_alt,
                ':upload_description' => $upload_description,
                ':upload_time' => $upload_time
            ));
            return $this->db->lastInsertId();
        }
        catch (Exception $e){
            throw new Exception("Saving error! Try again!", 201);
        }
    }

    public function addUploaderTree(){

    }

    public function updateUploader(){

    }

    public function updateUploaderTree(){

    }

    public function deleteUploaderById(){

    }

    public function deleteUploaderTreeById(){

    }
}